<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules"
             class="login-form flex flex-col border-[#d71920] w-[400px] h-[380px]
              absolute top-[150px] [@media(min-width:1700px)]:top-[248px] left-[290px] bg-[rgba(107,188,255,0.1)]
 ">

      <div class="text-[36px] leading-9">欢迎登录</div>
      <el-form-item prop="username" class="!mt-[48px] !h-[40px]">
        <el-input class="" v-model="loginForm.username" type="text" auto-complete="off" placeholder="用户名">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon text-blue-400"  />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" class="!mt-[24px]">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          show-password
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon text-blue-400" />
        </el-input>
      </el-form-item>
<!--      <el-form-item prop="code">-->
<!--        <el-input-->
<!--          v-model="loginForm.code"-->
<!--          auto-complete="off"-->
<!--          placeholder="验证码"-->
<!--          style="width: 63%"-->
<!--          @keyup.enter.native="handleLogin"-->
<!--        >-->
<!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
<!--        </el-input>-->
<!--        <div class="login-code">-->
<!--          <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
<!--        </div>-->
<!--      </el-form-item>-->
<!--      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
      <el-form-item style="width:100%;" class="mt-[40px]">
        <el-button
          :loading="loading"
          size="medium" class="!rounded-[6px]"
          type="primary"
          style="width:100%; border-radius: 6px;"
          @click.native.prevent="handleLogin"
        >
          <span class="!text-[16px]" v-if="!loading">登 录</span>
          <span class="!text-[16px]" v-else>登 录 中...</span>
        </el-button>

      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer hidden">
      <span> Copyright © 2024- <span id="current-year"></span>2025 HP VISION.   All rights reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'


export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      sysName: "",
      sysCopyright: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
      },
      loginRules: {
        username: [
          { required: true, trigger: "change", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "change", message: "密码不能为空" }
        ],
        // code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    const titleElement = document.getElementById("name");
    titleElement.innerText = '登  录';
    // document.getElementById('current-year').textContent = new Date().getFullYear();
    this.getCookie();
    //系统名称
    // this.getConfigKey("sys.name").then(response => {
    //   this.sysName = response.msg;
    //   this.$store.dispatch('app/getNAME',response.msg)
    //   localStorage.setItem("hpyName",response.msg)
    // });
    // //系统版权
    // this.getConfigKey("sys.copyright").then(response => {
    //   this.sysCopyright = response.msg;
    // });
    //系统logo
    // this.getConfigKey("sys.logo").then(response => {
    //   // this.$store.commit('SYSLOGO', response.msg) // 提交 setMessage mutation
    //   console.log(response)
    //   this.$store.dispatch('app/getLOGO',response.msg)
    //   localStorage.setItem("hpyLogo",response.msg)
    // });

  },
  methods: {

    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
* {
  box-sizing: border-box;
}
.login {
  //display: flex;
  //justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/logo/BG.png");
  background-size: cover;
}
.logo {
  background-image: url("../assets/logo/LOGO.png");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 20px 20px 20px 20px;
  border: 2px solid #FFFFFF;
  width: 400px;
  padding: 48px 32px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  z-index: 999;
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(75, 72, 72, 0.58);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
